﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Security.Permissions;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using ESF.General;

#endregion


namespace ESF.Web.ServerControls
{
	[AspNetHostingPermission(SecurityAction.Demand,
        Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, 
        Level = AspNetHostingPermissionLevel.Minimal),
	ToolboxData("<{0}:CaptureDate runat=\"server\"></{0}:CaptureDate>")]
	public class CaptureDate : WebControl, INamingContainer
	{
		#region Miembros

		private Label lblTitle;
		private Label lblTitleStartDate;
		private Label lblTitleEndDate;

		private Label lblInstructionsStartDate;
		private Label lblInstructionsEndDate;

		private TextBox txtStartDate;
		private TextBox txtEndDate;

		private Image imgStartCalendar;
		private Image imgEndCalendar;

		private RequiredFieldValidator rfvtxtStartDate;
		private RequiredFieldValidator rfvtxtEndDate;

		private CalendarExtender calStartExtender;
		private CalendarExtender calEndExtender;

		private FilteredTextBoxExtender extFilterStartDate;
		private FilteredTextBoxExtender extFilterEndDate;

		#endregion


		#region Propiedades

		public string ValidationGroup
		{
			set
			{
				EnsureChildControls ();

				rfvtxtEndDate.ValidationGroup = value;

				rfvtxtStartDate.ValidationGroup = value;
			}
		}


		public bool Required
		{
			set
			{
				EnsureChildControls ();
				
				rfvtxtStartDate.Enabled = value;
				rfvtxtEndDate.Enabled = value;
			}
		}


		public string Title
		{
			set
			{
				EnsureChildControls ();

				lblTitle.Text = value;
			}
		}
		

		public string StartLabel
		{
			set
			{
				EnsureChildControls ();
				
				lblTitleStartDate.Text = value;
			}
		}


		public string EndLabel
		{
			set
			{
				EnsureChildControls ();
				
				lblTitleEndDate.Text = value;
			}
		}


		public string DateFormat
		{
			set
			{
				EnsureChildControls ();
				
				calStartExtender.Format = value;
				calEndExtender.Format = value;
			}
		}


		public DateTime? StartDate
		{
			set
			{
				if (value == null)
				{
					txtStartDate.Text = string.Empty;
				}
				else
				{
					txtStartDate.Text = value.Value.ToString (calStartExtender.Format);
				}
			}
			get
			{
				return TextHelper.GetNullableDateTime (txtStartDate.Text, calStartExtender.Format);
			}
		}


		public DateTime? EndDate
		{
			set
			{
				if (value == null)
				{
					txtEndDate.Text = string.Empty;
				}
				else
				{
					txtEndDate.Text = value.Value.ToString (calEndExtender.Format);
				}
			}
			get
			{
				return TextHelper.GetNullableDateTime (txtEndDate.Text, calEndExtender.Format);
			}
		}

		#endregion


		#region Funciones

		protected override void CreateChildControls()
		{
			lblTitle = new Label ();
			lblTitle.ID = "lblTitle";

			lblTitleEndDate = new Label ();
			lblTitleEndDate.ID = "lblTitleEndDate";

			lblTitleStartDate = new Label ();
			lblTitleStartDate.ID = "lblTitleStartDate";

			txtStartDate = new TextBox ();
			txtStartDate.ID = "txtStartDate";
			txtStartDate.MaxLength = 10;

			txtEndDate = new TextBox ();
			txtEndDate.ID = "txtEndDate";
			txtEndDate.MaxLength = 10;

			rfvtxtEndDate = new RequiredFieldValidator ();
			rfvtxtEndDate.ID = "rfvtxtEndDate";
			rfvtxtEndDate.ControlToValidate = txtEndDate.ID;
			rfvtxtEndDate.Enabled = false;
			rfvtxtEndDate.ErrorMessage = string.Format ("Diligencie {0}", lblTitleEndDate.Text);
			rfvtxtEndDate.Text = "*";

			rfvtxtStartDate = new RequiredFieldValidator ();
			rfvtxtStartDate.ID = "rfvtxtStartDate";
			rfvtxtStartDate.ControlToValidate = txtStartDate.ID;
			rfvtxtStartDate.Enabled = false;
			rfvtxtStartDate.ErrorMessage = string.Format ("Diligencie {0}", lblTitleStartDate.Text);
			rfvtxtStartDate.Text = "*";

			imgEndCalendar = new Image ();
			imgEndCalendar.ID = "imgEndCalendar";
			imgEndCalendar.CssClass = "imgCalendar";
			
			imgStartCalendar = new Image ();
			imgStartCalendar.ID = "imgStartCalendar";
			imgStartCalendar.CssClass = "imgCalendar";

			calStartExtender = new CalendarExtender ();
			calStartExtender.ID = "calStartExtender";
			calStartExtender.TargetControlID = txtStartDate.ID;
			calStartExtender.PopupButtonID = imgStartCalendar.ID;

			calEndExtender = new CalendarExtender ();
			calEndExtender.ID = "calEndExtender";
			calEndExtender.TargetControlID = txtEndDate.ID;
			calEndExtender.PopupButtonID = imgEndCalendar.ID;

			lblInstructionsStartDate = new Label ();
			lblInstructionsStartDate.ID = "lblInstructionsStartDate";
			lblInstructionsStartDate.CssClass = "instructions";
			
			lblInstructionsEndDate = new Label ();
			lblInstructionsEndDate.ID = "lblInstructionsEndDate";
			lblInstructionsEndDate.CssClass = "instructions";
			
			extFilterStartDate = new FilteredTextBoxExtender ();
			extFilterStartDate.ID = "extFilterStartDate";
			extFilterStartDate.ValidChars = "0123456789-";
			extFilterStartDate.TargetControlID = txtStartDate.ID;

			extFilterEndDate = new FilteredTextBoxExtender ();
			extFilterEndDate.ID = "extFilterEndDate";
			extFilterEndDate.ValidChars = "0123456789-/";
			extFilterEndDate.TargetControlID = txtEndDate.ID;

			Controls.Add (lblTitleEndDate);
			Controls.Add (lblTitleStartDate);
			Controls.Add (txtStartDate);
			Controls.Add (txtEndDate);
			Controls.Add (rfvtxtEndDate);
			Controls.Add (rfvtxtStartDate);
			Controls.Add (imgEndCalendar);
			Controls.Add (imgStartCalendar);
			Controls.Add (calStartExtender);
			Controls.Add (calEndExtender);
			Controls.Add (lblInstructionsStartDate);
			Controls.Add (lblInstructionsEndDate);
			Controls.Add (lblTitle);
			Controls.Add (extFilterStartDate);
			Controls.Add (extFilterEndDate);
		}


		public void Clear ()
		{
			EnsureChildControls ();

			txtEndDate.Text = string.Empty;
			txtStartDate.Text = string.Empty;
		}


		public void Verify ()
		{
			if (!string.IsNullOrEmpty (lblTitleStartDate.Text))
			{
				if (!string.IsNullOrEmpty (lblTitleEndDate.Text))
				{
					ESF.General.Validate.VerifyDateRange (
						lblTitleStartDate.Text, lblTitleEndDate.Text,
						txtStartDate.Text, txtEndDate.Text); 
				}
				else
				{
					ESF.General.Validate.VerifyDate (lblTitleStartDate.Text, txtStartDate.Text);
				}
			}
		}

		#endregion


		#region Render

		protected override void OnPreRender(EventArgs e)
		{
			if (string.IsNullOrEmpty (calStartExtender.Format))
			{
				calStartExtender.Format = "yyyy-MM-dd";
				calEndExtender.Format = "yyyy-MM-dd";
			}

			if (string.IsNullOrEmpty (lblTitle.Text))
			{
				lblTitle.Text = "Fecha";
			}

			if (string.IsNullOrEmpty (lblInstructionsStartDate.Text))
			{
				lblInstructionsStartDate.Text = "Desde";
			}

			lblInstructionsStartDate.Text = string.Format ("Ejemplo: {0}", DateTime.Now.AddDays (-7).ToString (calStartExtender.Format));
			lblInstructionsEndDate.Text = string.Format ("Ejemplo: {0}", DateTime.Now.AddDays (-7).ToString (calEndExtender.Format));
		}

		protected override void Render(HtmlTextWriter pWriter)
		{
				pWriter.AddAttribute (HtmlTextWriterAttribute.Class, string.IsNullOrEmpty (CssClass) ? "divField" : CssClass);
			pWriter.RenderBeginTag (HtmlTextWriterTag.Div);

				#region Solo una 

				if (string.IsNullOrEmpty (lblInstructionsEndDate.Text))
				{
					pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "label");
					pWriter.RenderBeginTag (HtmlTextWriterTag.Div);

						lblTitle.RenderControl (pWriter);
						rfvtxtStartDate.RenderControl (pWriter);
			
					pWriter.RenderEndTag ();

					pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "field");
					pWriter.RenderBeginTag (HtmlTextWriterTag.Div);

						txtStartDate.RenderControl (pWriter);
						imgStartCalendar.RenderControl (pWriter);
						lblInstructionsStartDate.RenderControl (pWriter);
						calStartExtender.RenderControl (pWriter);
						extFilterStartDate.RenderControl (pWriter);

					pWriter.RenderEndTag ();
				}
				
				#endregion

				#region Dos campos

				else
				{
						pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "label");
					pWriter.RenderBeginTag (HtmlTextWriterTag.Div);

						lblTitle.RenderControl (pWriter);

					pWriter.RenderEndTag ();

						pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "divSubField");
					pWriter.RenderBeginTag (HtmlTextWriterTag.Div);

							pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "label");
						pWriter.RenderBeginTag (HtmlTextWriterTag.Div);

							lblTitleStartDate.RenderControl (pWriter);
							rfvtxtStartDate.RenderControl (pWriter);
			
						pWriter.RenderEndTag ();

						pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "field");
						pWriter.RenderBeginTag (HtmlTextWriterTag.Div);

							txtStartDate.RenderControl (pWriter);
							imgStartCalendar.RenderControl (pWriter);
							lblInstructionsStartDate.RenderControl (pWriter);
							calStartExtender.RenderControl (pWriter);
							extFilterStartDate.RenderControl (pWriter);

						pWriter.RenderEndTag ();

					pWriter.RenderEndTag ();

						pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "divSubField");
					pWriter.RenderBeginTag (HtmlTextWriterTag.Div);

							pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "label");
						pWriter.RenderBeginTag (HtmlTextWriterTag.Div);

							lblTitleEndDate.RenderControl (pWriter);
							rfvtxtEndDate.RenderControl (pWriter);
			
						pWriter.RenderEndTag ();

							pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "field");
						pWriter.RenderBeginTag (HtmlTextWriterTag.Div);

							txtEndDate.RenderControl (pWriter);
							imgEndCalendar.RenderControl (pWriter);
							lblInstructionsEndDate.RenderControl (pWriter);
							calEndExtender.RenderControl (pWriter);
							extFilterEndDate.RenderControl (pWriter);

						pWriter.RenderEndTag ();
				
					pWriter.RenderEndTag ();
				}

				#endregion

			pWriter.RenderEndTag ();
		}

		#endregion
	}
}
